// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>
#include <limits>

#include <pollux/common/base/bit_util.h>
#include <pollux/dwio/dwrf/utils/bit_iterator.h>

using namespace ::testing;

namespace kumo::pollux::dwrf::utils {

TEST(BulkBitIterator, Basic) {
  std::vector<char> charBuffer1{
      std::numeric_limits<char>::max(), 102, 40, -120};
  std::vector<char> charBuffer2{125, 85, 42, std::numeric_limits<char>::min()};

  BulkBitIterator<char> bulkIter{};
  bulkIter.addRawByteBuffer(charBuffer1.data());
  bulkIter.addRawByteBuffer(charBuffer2.data());

  for (size_t i = 0; i < 32; ++i) {
    bulkIter.loadNext();
    EXPECT_EQ(bits::isBitSet(charBuffer1.data(), i), bulkIter.hasValueAt(0))
        << "Mismatch for first iter at " << i << "-th element";
    EXPECT_EQ(bits::isBitSet(charBuffer2.data(), i), bulkIter.hasValueAt(1))
        << "Mismatch for second iter at " << i << "-th element";
  }

  std::vector<long> longBuffer1{
      12790, std::numeric_limits<long>::max(), 45, -1288};
  std::vector<long> longBuffer2{
      125, 85098, std::numeric_limits<long>::min(), -98009};

  BulkBitIterator<long> longBulkIter{};
  longBulkIter.addRawByteBuffer(longBuffer1.data());
  longBulkIter.addRawByteBuffer(longBuffer2.data());

  for (size_t i = 0; i < 256; ++i) {
    longBulkIter.loadNext();
    EXPECT_EQ(bits::isBitSet(longBuffer1.data(), i), longBulkIter.hasValueAt(0))
        << "Mismatch for first iter at " << i << "-th element";
    EXPECT_EQ(bits::isBitSet(longBuffer2.data(), i), longBulkIter.hasValueAt(1))
        << "Mismatch for second iter at " << i << "-th element";
  }
}
} // namespace kumo::pollux::dwrf::utils
